package com.jevons.geo_edit.model;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;

import com.google.gson.JsonObject;
import com.jevons.geo_edit.BR;
import com.jevons.muffin.utilcode.StringUtils;

import java.util.List;

/**
 * Copyright 2020 JxGIS
 *
 * @file GisLayerStyle
 * @auth linaisheng
 * Created on 2023/6/29.
 * Description：
 */
public class GisLayerStyle extends BaseObservable {


    private static final int DEFAULT_VALUE_POINT_TYPE = 0;
    private static final int DEFAULT_VALUE_POINT_STYLE = 0;
    private static final float DEFAULT_VALUE_POINT_SIZE = 1;

    private static final int DEFAULT_VALUE_LINE_TYPE = 0;
    private static final String DEFAULT_VALUE_LINE_COLOR = "#3290FC";
    private static final float DEFAULT_VALUE_LINE_OPACITY = 1;
    private static final float DEFAULT_VALUE_LINE_WIDTH = 2;

    private static final int DEFAULT_VALUE_FILL_TYPE = 0;
    private static final String DEFAULT_VALUE_FILL_COLOR = "#FF6666";
    private static final float DEFAULT_VALUE_FILL_OPACITY = 0.8f;

    private static final float DEFAULT_VALUE_FONT_SIZE = 12;
    private static final String DEFAULT_VALUE_FONT_FILL_COLOR = "#2a2a2a";
    private static final String DEFAULT_VALUE_FONT_STROKE_COLOR = "#c2c2c2";

    private String projectId;
    private String layerId;

    private int pointType = DEFAULT_VALUE_POINT_TYPE;
    private float pointSize = DEFAULT_VALUE_POINT_SIZE;
    private int pointStyle = DEFAULT_VALUE_POINT_STYLE;

    private int lineType = DEFAULT_VALUE_LINE_TYPE;
    private String lineColor = DEFAULT_VALUE_LINE_COLOR;
    private float lineOpacity = DEFAULT_VALUE_LINE_OPACITY;
    private float lineWidth = DEFAULT_VALUE_LINE_WIDTH;

    private int fillType = DEFAULT_VALUE_FILL_TYPE;
    private String fillColor =  DEFAULT_VALUE_FILL_COLOR;
    private float fillOpacity = DEFAULT_VALUE_FILL_OPACITY;


    private String showAnnotationKey = "";
    private float fontSize = DEFAULT_VALUE_FONT_SIZE;
    private String fontFamily;
    private int fontBold;
    private int fontItalic;
    private String fontFillColor = DEFAULT_VALUE_FONT_FILL_COLOR;
    private float fontStrokeWidth;
    private String fontStrokeColor = DEFAULT_VALUE_FONT_STROKE_COLOR;



    private long updateTime;

    public GisLayerStyle(String projectId, String layerId) {
        this.projectId = projectId;
        this.layerId = layerId;
    }

    public GisLayerStyle(
            String projectId, String layerId,
            int pointType, float pointSize, int pointStyle,
            int lineType, String lineColor, float lineOpacity, float lineWidth,
            int fillType, String fillColor, float fillOpacity,
            long updateTime) {
        this.projectId = projectId;
        this.layerId = layerId;
        this.pointType = pointType;
        this.pointSize = pointSize;
        this.pointStyle = pointStyle;
        this.lineType = lineType;
        this.lineColor = lineColor;
        this.lineOpacity = lineOpacity;
        this.lineWidth = lineWidth;
        this.fillType = fillType;
        this.fillColor = fillColor;
        this.fillOpacity = fillOpacity;
        this.updateTime = updateTime;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public String getLayerId() {
        return layerId;
    }

    public void setLayerId(String layerId) {
        this.layerId = layerId;
    }


    @Bindable
    public int getPointType() {
        return pointType;
    }

    public void setPointType(int pointType) {
        this.pointType = pointType;
        notifyPropertyChanged(BR.pointType);
    }

    @Bindable
    public float getPointSize() {
        return pointSize != 0 ? pointSize : DEFAULT_VALUE_POINT_SIZE;
    }

    public void setPointSize(float pointSize) {
        this.pointSize = pointSize;
        notifyPropertyChanged(BR.pointSize);
    }

    @Bindable
    public int getPointStyle() {
        return pointStyle;
    }

    public void setPointStyle(int pointStyle) {
        this.pointStyle = pointStyle;
        notifyPropertyChanged(BR.pointStyle);
    }

    @Bindable
    public int getLineType() {
        return lineType;
    }

    public void setLineType(int lineType) {
        this.lineType = lineType;
        notifyPropertyChanged(BR.lineType);
    }

    @Bindable
    public String getLineColor() {
        return StringUtils.isNotEmpty(lineColor) ? lineColor : DEFAULT_VALUE_LINE_COLOR;
    }

    public void setLineColor(String lineColor) {
        this.lineColor = lineColor;
        notifyPropertyChanged(BR.lineColor);
    }

    @Bindable
    public float getLineOpacity() {
        if (0 <= lineOpacity && lineOpacity <= 1) {
            return lineOpacity;
        } else {
            return DEFAULT_VALUE_LINE_OPACITY;
        }
    }

    public void setLineOpacity(float lineOpacity) {
        this.lineOpacity = lineOpacity;
        notifyPropertyChanged(BR.lineOpacity);
    }

    @Bindable
    public float getLineWidth() {
        return lineWidth != 0 ? lineWidth : DEFAULT_VALUE_LINE_WIDTH;
    }

    public void setLineWidth(float lineWidth) {
        this.lineWidth = lineWidth;
        notifyPropertyChanged(BR.lineWidth);
    }

    @Bindable
    public int getFillType() {
        return fillType;
    }

    public void setFillType(int fillType) {
        this.fillType = fillType;
        notifyPropertyChanged(BR.fillType);
    }

    @Bindable
    public String getFillColor() {
        return StringUtils.isNotEmpty(fillColor) ? fillColor : DEFAULT_VALUE_FILL_COLOR;
    }

    public void setFillColor(String fillColor) {
        this.fillColor = fillColor;
        notifyPropertyChanged(BR.fillColor);
    }

    @Bindable
    public float getFillOpacity() {
        if (0 <= fillOpacity && fillOpacity <= 1) {
            return fillOpacity;
        } else {
            return DEFAULT_VALUE_FILL_OPACITY;
        }
    }

    public void setFillOpacity(float fillOpacity) {
        this.fillOpacity = fillOpacity;
        notifyPropertyChanged(BR.fillOpacity);
    }

    @Bindable
    public String getShowAnnotationKey() {
        return showAnnotationKey;
    }

    public void setShowAnnotationKey(String showAnnotationKey) {
        this.showAnnotationKey = showAnnotationKey;
        notifyPropertyChanged(BR.showAnnotationKey);
    }

    @Bindable
    public float getFontSize() {
        return fontSize != 0 ? fontSize : DEFAULT_VALUE_FONT_SIZE;
    }

    public void setFontSize(float fontSize) {
        this.fontSize = fontSize;
        notifyPropertyChanged(BR.fontSize);
    }

    @Bindable
    public String getFontFamily() {
        return fontFamily;
    }

    public void setFontFamily(String fontFamily) {
        this.fontFamily = fontFamily;
        notifyPropertyChanged(BR.fontFamily);
    }

    @Bindable
    public int getFontBold() {
        return fontBold;
    }

    public void setFontBold(int fontBold) {
        this.fontBold = fontBold;
        notifyPropertyChanged(BR.fontBold);
    }

    @Bindable
    public int getFontItalic() {
        return fontItalic;
    }

    public void setFontItalic(int fontItalic) {
        this.fontItalic = fontItalic;
        notifyPropertyChanged(BR.fontItalic);
    }

    @Bindable
    public String getFontFillColor() {
        return StringUtils.isNotEmpty(fontFillColor) ? fontFillColor : DEFAULT_VALUE_FONT_FILL_COLOR;
    }

    public void setFontFillColor(String fontFillColor) {
        this.fontFillColor = fontFillColor;
        notifyPropertyChanged(BR.fontFillColor);
    }

    @Bindable
    public float getFontStrokeWidth() {
        return fontStrokeWidth;
    }

    public void setFontStrokeWidth(float fontStrokeWidth) {
        this.fontStrokeWidth = fontStrokeWidth;
        notifyPropertyChanged(BR.fontStrokeWidth);
    }

    @Bindable
    public String getFontStrokeColor() {
        return StringUtils.isNotEmpty(fontStrokeColor) ? fontStrokeColor : DEFAULT_VALUE_FONT_STROKE_COLOR;
    }

    public void setFontStrokeColor(String fontStrokeColor) {
        this.fontStrokeColor = fontStrokeColor;
        notifyPropertyChanged(BR.fontStrokeColor);
    }

    public long getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(long updateTime) {
        this.updateTime = updateTime;
    }


    public String getUploadLayerStyleStr() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("pointType", pointType);
        jsonObject.addProperty("pointSize", pointSize);
        jsonObject.addProperty("pointStyle", pointStyle);
        jsonObject.addProperty("strokeType", lineType);
        jsonObject.addProperty("strokeWidth", lineWidth);
        jsonObject.addProperty("strokeColor", lineColor);
        jsonObject.addProperty("strokeOpacity", lineOpacity);
        jsonObject.addProperty("fillType", fillType);
        jsonObject.addProperty("fillColor", fillColor);
        jsonObject.addProperty("fillOpacity", fillOpacity);
        return jsonObject.toString();
    }

    public String getUploadLabelStyleStr(List<GisField> fields) {
        JsonObject jsonObject = new JsonObject();
        String showKey = "";
        if (StringUtils.isNotEmpty(showAnnotationKey)
                && fields != null && fields.size() > 0) {
            for (GisField field : fields) {
                if (showAnnotationKey.equals(field.getId())) {
                    showKey = field.getFieldName();
                }
            }
        }
        jsonObject.addProperty("showKey", showKey);
        jsonObject.addProperty("fontSize", fontSize);
        jsonObject.addProperty("fontFamily", fontFamily);
        jsonObject.addProperty("fontBold", fontBold);
        jsonObject.addProperty("fontStyle", fontItalic);
        jsonObject.addProperty("fontFillColor", fontFillColor);
        jsonObject.addProperty("fontStrokeColor", fontStrokeColor);
        jsonObject.addProperty("fontStrokeWidth", fontStrokeWidth);
        return jsonObject.toString();
    }


}